package com.aurora.leetcode.answer;

/**
 * @author Aurora
 * createTime 2022.11.30 09:06:39
 * @className SqrtResult_69.java
 */
public class SqrtResult_69 {
    //解法一：遍历，会超出时间限制
    public void mySqrt1(int x) {
        Long half = new Long(x);
        while(half*half > x){//对于超出int范围的数  此判断条件会失效
            half -= 1;
        }
        int result = half.intValue();
        System.out.println(result);
    }


    /**解法二：
     * 利用双指
     * */
    public  int mySqrt2(int x) {
        if(x == 1)return x;
        Long result = 0L;
        Long last = 0L;
        Long left = 0L;
        Long right =(long)x;
        while(left < right){
//            result = (right + left) / 2;
            result = (right + left) >>> 1;//优化解法
            if(last.equals(result)){
                break;
            }
            if(result * result > x){
                right = result;
            }else{
                left = result;
            }
            last = result;
        }
        System.out.println("计算结果为："+result);
        return result.intValue();
    }


}
